package com.yjck.fm.spend.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.yjck.fm.common.Constants;
import com.yjck.fm.common.QueryResult;
import com.yjck.fm.spend.service.SpendTypeService;

public class SpendTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Map<String, Object> map = new HashMap<String, Object>();
	private SpendTypeService spendTypeService;
	
	/**
	 * 修改删除用的编号
	 */
	private String sid;
	/**
	 * 增加修改的字段名称
	 */
	private String typeName;
	private int start;
	private int limit;


	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	public String getType() throws Exception {
	QueryResult queryResult = spendTypeService.getTypes(this.typeName,this.start,this.limit);
	map.put("result", queryResult.getResult());	
	map.put("count", queryResult.getCount());
		return SUCCESS;
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addType() throws Exception {
		Integer uid = (Integer) ServletActionContext.getRequest().getSession().getAttribute("id");
		if(!spendTypeService.isExist(typeName)){
			boolean result = spendTypeService.addType(this.typeName,uid.intValue());
			if(result){
				map.put("result", Constants.TRUE);		
			}else{
				map.put("result", Constants.FALSE);
			}
		}else{
			map.put("result", Constants.ERROR);
		}
			return SUCCESS;
		}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String modifyType() throws Exception {
		Integer uid = (Integer) ServletActionContext.getRequest().getSession().getAttribute("id");
		boolean flag = spendTypeService.isExist(this.typeName);
		if(!flag){
			boolean result = spendTypeService.modifyType(this.typeName,this.sid,uid.intValue());
			if(result){
				map.put("result", Constants.TRUE);		
			}else{
				map.put("result", Constants.FALSE);
			}			
		}else{
			map.put("result", Constants.ERROR);
		}
			return SUCCESS;
		}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delType() throws Exception {
		boolean result = spendTypeService.delType(this.sid);
		if(result){
			map.put("result", Constants.TRUE);		
		}else{
			map.put("result", Constants.FALSE);
		}
			return SUCCESS;
		}

	public String getTypeName() {
		return typeName;
	}
	public void setSpendTypeService(SpendTypeService spendTypeService) {
		this.spendTypeService = spendTypeService;
	}
	
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public void setSid(String sid) {
		this.sid = sid;
	}
	public String getSid() {
		return sid;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getStart() {
		return start;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getLimit() {
		return limit;
	}


}
